// packageA/pages/received-bidding/received-bidding.js
import {Bidding} from '../../../models/bidding'
import {check} from '../../../util/identityCheck'

let biddingModel = new Bidding()

Page({
  data: {
    tabs: ['未查看', '已查看'],
    tabIndex: 0,
    timeStamp: 0,
    data:[],
    page: 1,
    showLoading: false
  },

  onShow() {
    if(!check())return
    this.init()
  },

  onPullDownRefresh () {
    this.setData({
      page: 1
    })
    this.getReceivedBidding()
  },

  onReachBottom () {
    this.setData({
      page: this.data.page + 1
    })
    this.getReceivedBidding('more')
  },

  chooseTab (event) {
    let type = event.type
    let timeStamp = event.timeStamp
    let index =  event.detail.current || event.target.dataset.index || 0
    if (type === 'change' && (timeStamp - this.data.timeStamp) < 50) {
      return
    }
    this.setData({
      tabIndex: index,
      timeStamp,
      page: 1
    })

    this.getReceivedBidding()
  },

  toNewPage (event) {
    let {id, path, relationid} = event.currentTarget.dataset
    wx.navigateTo({
      url: `/pages/${path}/${path}?id=${id}&from=received`
    })
    this.updateReadState(relationid)
  },

  getReceivedBidding (type) {
    biddingModel.getReceivedBidding({
      page: this.data.page,
      isRead: this.data.tabIndex
    }, res => {
      this.setData({
        data: type == 'more' ? [...this.data.data, ...res.rows] : res.rows,
        showLoading: res.rows.length >= 10
      })
    })
  },

  updateReadState (relationId) {
    biddingModel.updateReadState({
      relationId
    })
  },

  init () {
    this.getReceivedBidding()
  }
})
